#!/usr/bin/ruby

require 'rubygems'
require 'pp'
require 'xmlrpc/client'
require 'optparse'
require 'yaml' # Assumed in future examples


## hash for all the options
options = {}
options[:username] = ""
options[:password] = ""
options[:project] = ""
options[:server] = ""
options[:filename] = ""

## Get options from YAML
## 
CONFIG = YAML::load(File.read('options.yaml'))
options[:username] = CONFIG['settings']['username']
options[:password] = CONFIG['settings']['password']
options[:project] = CONFIG['settings']['project']
options[:server] = CONFIG['settings']['server']
options[:filename] = CONFIG['settings']['filename']

## Set up the option parser
optparse = OptionParser.new do|opts|
   opts.banner = "Usage: ticketgen.rb --user [user] --pass [pass] --project [project] --server [server] --filename [ticketfile]" 

   ## Gather command-line options
   opts.on('-h', '--help', 'Help Screen') do
      puts opts
      exit
   end
   
   opts.on('-v', '--verbose', 'Verbose Output') do
      options[:verbose] = true
      exit
   end

   opts.on('-u', '--user USER', String, 'Trac Username') do |username|
      options[:username]  = username
   end

   opts.on('-p', '--password PASSWORD', String, 'Trac Password') do |password|
      options[:password]  = password
   end  

   opts.on('-r', '--project PROJECT', String, 'Trac Project') do |project|
      options[:project]  = project
   end

   opts.on('-s', '--server SERVER', String, 'Trac Server') do |server|
      options[:server]  = server
   end
 
   opts.on('-f', '--filename FILENAME', String, 'Trac Tickets (filename)') do |filename|
      options[:filename] = filename
   end
end


# Parse the command-line. Remember there are two forms
# of the parse method. The 'parse' method simply parses
# ARGV, while the 'parse!' method parses ARGV and removes
# any options found there, as well as any parameters for
# the options. What's left is the list of files to resize.
optparse.parse!


## Gather interactively if not specified
if options[:username].to_s.empty? then
   puts "User:"
   options[:username] = gets
end

if options[:password].to_s.empty? then
   puts "Password:"
   options[:password] = gets
end

if options[:project].to_s.empty? then
   puts "Project:"
   options[:project] = gets
end

if options[:server].to_s.empty? then
   puts "Server:"
   options[:server] = gets
end

if options[:filename].to_s.empty? then
   puts "Filename:"
   options[:filename] = gets
end

## ensure we have strings
String username = options[:username]
String password = options[:password]
String server = options[:server]
String project = options[:project]

## remove newlines
username.chomp!
password.chomp!
server.chomp!
project.chomp!

## construct the project url
url = "https://" + username + ":" + password + "\@" + server + "/projects/" + project + "/login/xmlrpc"

##puts "DEBUG: URL: " + url + "\n\n\n"

# initialize the connection (username and password can be ommitted if not needed, but most of the time you will need them if anonymous doesn't have XMLRPC permissions)
server = XMLRPC::Client::new2(url)

## generate a ticket for each line of the flie
f = File.open(options[:filename], "r")

f.each_line do|line|
	
	if line[0].chr != "#"
		
		## Split the ticket
		ticket_string = line.split(",")
		
		ticketname = ticket_string[0]
		ticketname.chomp!
 
		if ticket_string[1] != nil
			ticketdesc = ticket_string[1] + " "
		else
			ticketdesc = " "
		end
	
		##puts "DEBUG: line[0]: " + line[0].chr + "\n";
##		puts "generating ticket: #{line}"		
	
		###FILES

		## point to where the files are located
                ticketdescfileprefix = "/home/jcran/secure/rapid7/projects/elementkweb-svn/tickets/owasp/OWASP_"
		
		## remove unecessary shiz from the ticket name
		ticketbasefilename = ticket_string[0]
##		puts "ticketbasefilename = " + ticketbasefilename

		ticketbasefilename1 = ticketbasefilename.gsub("ELearning - ", "")
		ticketbasefilename = ticketbasefilename1.gsub(" ","_")	       

##		puts "ticketbasefilename1 = " + ticketbasefilename1 
##		puts "ticketdescfileprefix = " + ticketdescfileprefix
##		puts "ticketbasefilename = " + ticketbasefilename
	
		## create desctiption filename if it exists
		ticketdescfilename = ticketdescfileprefix + ticketbasefilename
		ticketdescfilename.chomp!
		
##		puts "Debug: Description file " + ticketdescfilename
		
		### --- Files

		String filetext = ""
		if File.exist?(ticketdescfilename)
##			puts "Debug: File exists. Getting description from file - " + ticketdescfilename + "\n"
   			ticketdescfile = File.open(ticketdescfilename, "r") do |infile|
				while (line = infile.gets)
					filetext = filetext + line
				end
			end
##			puts "Debug: Description text" + filetext + "\n"
		
			ticketname = ticket_string[0];
			ticketdesc = filetext
		else
			puts "No File, using ticket string.\n"
		end
		
		puts "Debug: Generating Ticket\n"
		puts "Debug: Title " + ticketname + "\n"
		puts "Debug: Description:\n " + ticketdesc + "\n"
		##puts "\n\n\n\n"
		results = server.call("ticket.create",ticketname,ticketdesc,{},false)
	end
end


